<?php
class Pageley_Domain_BasicEntityTests extends Pageley_Test_Case {
	private $entity;
	private $subtypeEntity;
	
	protected function setUp() {
		$string = new Pageley_Domain_Attribute_Basic("string");
		$number = new Pageley_Domain_Attribute_Basic("number");
		$type = new Pageley_Domain_BasicType(
			"Pageley_Commerce",
			"Article",
			new Pageley_Util_ArrayMap(array(
				"name" => $string,
				"price" => $number
			))
		);
		$this->entity = $type->newInstance();
		
		$subtype = new Pageley_Domain_BasicType(
			"Pageley_Commerce",
			"ClassifiedArticle",
			new Pageley_Util_ArrayMap(array(
				"classification" => $string
			)),
			$type
		);
		$this->subtypeEntity = $subtype->newInstance();
	}
	
	public function testGetType() {
		$type = $this->entity->getType();
		$this->assertEquals("Article", $type->getName());
		$this->assertEquals("Pageley_Commerce", $type->getNamespace());
	}
	
	public function testSetAndGet() {
		$value = $this->entity->get("name");
		$this->assertNull($value);
		
		try {
			$value = $this->entity->get("funkyAttribute");
			$this->fail("Expected Pageley_Domain_IllegalAttributeException");
		} catch(Pageley_Domain_IllegalAttributeException $e) {
			// sorry, no funky attributes today
		}
		
		$this->entity->set("name", "Llama Spray");
		$value = $this->entity->get("name");
		$this->assertEquals("Llama Spray", $value);
		
		try {
			$this->entity->set("funkyAttribute", "Funky Value");
			$this->fail("Expected Pageley_Domain_IllegalAttributeException");
		} catch(Pageley_Domain_IllegalAttributeException $e) {
			// sorry, no funky attributes today
		}
	}
	
	public function testAttributeInheritance() {
		$this->subtypeEntity->set("name", "Llama Spray");
		$this->subtypeEntity->set("classification", "non-funky");
		
		$name = $this->subtypeEntity->get("name");
		$classification = $this->subtypeEntity->get("classification");
		
		$this->assertEquals("Llama Spray", $name);
		$this->assertEquals("non-funky", $classification);
	}
}